package com.ruan.consumer;

import com.rabbitmq.client.Channel;
import com.ruan.utils.Auth0JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * @ClassName:EmailListener
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/9/22 16:35
 **/
@Slf4j
@Component
public class EmailListener {

    @RabbitListener(queues = "email-queue")
    public void listenerEmail(Message message, Channel channel) throws IOException {
        // 处理消息逻辑
        byte[] body = message.getBody();
        String bodyString = new String(body, StandardCharsets.UTF_8);

        Boolean flag = SendActivation(bodyString);

        if (flag){
            log.info("邮件发送成功");
            // 手动确认消息处理成功
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }else {
            log.info("邮件发送失败");
            // 手动确认消息处理成功
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

    private Boolean SendActivation(String email) {
        //发送的时候生成token
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("username", email);
        String token = Auth0JwtUtils.sign(userMap);
        // 判断邮箱的类型
        if (email.endsWith("@qq.com")) {
            return sendQQEmail(email,token);
        } else if (email.endsWith("@163.com")) {
            return send163Email(email,token);
        } else{
            return false;
        }

    }


    private Boolean send163Email(String email1, String token) {
        try{
            HtmlEmail email=new HtmlEmail();
            email.setCharset("utf-8");
            email.addTo(email1);
            //不同邮箱开启smtp
            email.setHostName("smtp.163.com");
            email.setSmtpPort(465);
            email.setSSLOnConnect(true);
            email.setFrom("13145229762@163.com");
            //下面第二个参数是写授权码，不是密码，不同也不一样
            email.setAuthentication("13145229762@163.com", "AWGNNZJKTUKRTZFR");
            email.setSubject("账号激活");
            //直接访问邮箱激活
            String link="http://localhost:8081/activation?token="+token;
            //拼成链接可以点击
            String content="<a href='"+link+"'>点击激活</a>";
            email.setMsg(content);
            email.send();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    private Boolean sendQQEmail(String email1, String token){
        try{
            HtmlEmail email=new HtmlEmail();
            email.setCharset("utf-8");
            email.addTo(email1);
            //不同邮箱开启smtp
            email.setHostName("smtp.qq.com");
            email.setSmtpPort(465);
            email.setSSLOnConnect(true);
            email.setFrom("594993948@qq.com");
            //下面第二个参数是写授权码，不是密码，不同也不一样
            email.setAuthentication("594993948@qq.com","yozbqfrhfqbmbbec");
            email.setSubject("账号激活");
            //直接访问邮箱激活
            String link="http://localhost:8081/activation?token="+token;
            //拼成链接可以点击
            String content="<a href='"+link+"'>点击激活</a>";
            email.setMsg(content);
            email.send();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
